package com.pine.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class PostUtil {

    public String send(String userPhone,String miKangPhone) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd%HH:mm:ss");

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MINUTE, 3);

        // 米糠小号
        String virtualnumber  = miKangPhone ;
        // AX  A方手机号
        String aparty  = userPhone;
        //显示虚拟号码
        String display_cal = "0";

        //当前时间戳
        String time = String.valueOf(System.currentTimeMillis());

        String endDate = simpleDateFormat.format(calendar.getTime());

        System.out.println("time:"+time +"-----------enDate:"+calendar.getTime());
        //Appkey
        String  url_A_x ="http://api.open.zhaopin.com/platform/org/get?access_token=551c619ef13c45debe92a64880f5e1cdlzJv&orgid=75620267";

        String json_send_sms = "{\"display_call\":\"" + display_cal + "}" ;

        URL url = new URL(url_A_x);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        httpURLConnection.setRequestMethod("GET");

        httpURLConnection.setDoOutput(true);

        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        OutputStream out = httpURLConnection.getOutputStream();

        out.write(json_send_sms.getBytes("UTF-8"));

        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            System.out.println("recv msg:\t"+EncodingTool.UnicodeToUtf8(resultBuffer.toString()));
            return EncodingTool.UnicodeToUtf8(resultBuffer.toString());
        } catch (Exception e) {
            throw e;
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            httpURLConnection.disconnect();
        }
    }
}
